package com.srit.map.web.action;

import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.srit.core.struts2.action.BaseAction;
import com.srit.map.business.bean.PieBean;
import com.srit.map.business.bean.Resource;
import com.srit.map.business.service.ChartService;

/**
 * @author chenxiling
 *图表操作action
 */
public class ChartAction extends BaseAction{
	/**
	 * 
	 */
	@Autowired(required = true)
	private ChartService chartService;
	
	public String page(){
		return "jump";
	}
	public String chartJson() throws JSONException{
		StringBuilder sb = new StringBuilder();
		List<Resource> list = chartService.getTown();
		List<PieBean> _list = new ArrayList<PieBean>();
		float allNum = chartService.getCompanyCountByArea(null);//得到所有企业数
		for(Resource resource:list){
			PieBean pie = new PieBean();
			String area = resource.getValue();
			float num = chartService.getCompanyCountByArea(area);//得到地区内的企业数
			pie.setName(area);
			float y = (num/allNum)*100;
			pie.setY(y);
			_list.add(pie);
		}
		String str1=JSONUtil.serialize(_list);
//		str1 = str1.replace("\"name\"", "name");
//		str1 = str1.replace("\"sliced\"", "sliced");
//		str1 = str1.replace("\"selected\"", "selected");
//		str1 = str1.replace("\"y\"", "y");
		sb.append(str1);
		response.setContentType("application/json;charset=GBK");
		PrintStream out=new PrintStream(System.out,true);
		try {
			out = new PrintStream(response.getOutputStream());
		} catch (IOException e) {		
			e.printStackTrace();
		}//获取out输出对象
			String output = sb.toString();
		
			out.print(output);
			out.flush();
			out.close();
		return null;
	}
}
